import com.suning.api.DefaultSuningClient;
import com.suning.api.SuningRequest;
import com.suning.api.SuningResponse;
import com.suning.api.entity.selfmarket.SaleFormQueryRequest;
import com.suning.api.entity.selfmarket.SaleFormQueryResponse;
import com.suning.api.entity.selfmarket.StockFormQueryRequest;
import com.suning.api.entity.selfmarket.StockFormQueryResponse;
import com.suning.api.exception.SuningApiException;

import java.util.concurrent.CountDownLatch;

/**
 * Created by Fuang on 2018-1-11.
 */
public class Test {

    public static void main(String[] args) {
        CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch doneSignal = new CountDownLatch(20);

        for (int i = 0; i < 20; ++i) // create and start threads
            new Thread(new Worker(startSignal, doneSignal)).start();

        System.out.println("startSignal = " + startSignal.getCount() + "--------------doneSignal = " +doneSignal.getCount());
        startSignal.countDown();      // let all threads proceed
        System.out.println("startSignal = " + startSignal.getCount() + "--------------doneSignal = " +doneSignal.getCount());
        try {
            doneSignal.await();           // wait for all to finish
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("startSignal = " + startSignal.getCount() + "--------------doneSignal = " +doneSignal.getCount());
    }

    static class Worker implements Runnable {
        private final CountDownLatch startSignal;
        private final CountDownLatch doneSignal;

        Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
            this.startSignal = startSignal;
            this.doneSignal = doneSignal;
        }

        public void run() {
            try {
                startSignal.await();
                doWork();
                doneSignal.countDown();
            } catch (InterruptedException ex) {
            } // return;
        }

        void doWork() {
            System.out.println(Thread.currentThread().getName() + "----------startSignal = " + startSignal.getCount()
                    + "--------------doneSignal = " + doneSignal.getCount());
        }
    }

    public static void main2(String[] args) {
        int i = 0;
        System.out.println(i = 5);
    }

    public static void main1(String[] args) {
        StockFormQueryRequest request = new StockFormQueryRequest();
        request.setPlaceCode("L025");
        request.setPageNo(1);
        request.setPageSize(2);
        request.setBrandCode("10115515");

        request.setCheckParam(true);

        StockFormQueryResponse response = excute(request);

        System.out.println(response.getRspMap().get("sn_error"));

        stockQuery();

        saleQuery();
    }

    public static <T extends SuningResponse> T excute(SuningRequest<T> request) {
        String serverUrl = "https://open.suning.com/api/http/sopRequest";
        String appKey = "149b29c2a315c2ee05204753fc363424";
        String appSecret = "695bb2be5144cd91365e3dfb3e76bb9f";
        DefaultSuningClient client = new DefaultSuningClient(serverUrl, appKey,appSecret, "json");
        SuningResponse response = null;
        try {
            response = client.excute(request);
            System.out.println("返回json/xml格式数据 :" + response.getBody());
        } catch (SuningApiException e) {
            e.printStackTrace();
        }

        return (T) response;
    }

    public static void stockQuery() {
        StockFormQueryRequest request = new StockFormQueryRequest();
        request.setPlaceCode("L025");
        request.setPageNo(1);
        request.setPageSize(2);
        request.setBrandCode("10115515");

        request.setCheckParam(true);
        String serverUrl = "https://open.suning.com/api/http/sopRequest";
        String appKey = "149b29c2a315c2ee05204753fc363424";
        String appSecret = "695bb2be5144cd91365e3dfb3e76bb9f";
        DefaultSuningClient client = new DefaultSuningClient(serverUrl, appKey,appSecret, "json");
        try {
            StockFormQueryResponse response = client.excute(request);
            System.out.println("返回json/xml格式数据 :" + response.getBody());
        } catch (SuningApiException e) {
            e.printStackTrace();
        }
    }

    public static void saleQuery() {
        SaleFormQueryRequest request = new SaleFormQueryRequest();
        request.setStartTime("20140401");
        request.setEndTime("20140401");
        request.setBrandCode("10001373");
        request.setPageNo(1);
        request.setPageSize(2);
        //api入参校验逻辑开关，当测试稳定之后建议设置为 false 或者删除该行
        request.setCheckParam(true);
        String serverUrl = "https://open.suning.com/api/http/sopRequest";
        String appKey = "149b29c2a315c2ee05204753fc363424";
        String appSecret = "695bb2be5144cd91365e3dfb3e76bb9f";
        DefaultSuningClient client = new DefaultSuningClient(serverUrl, appKey,appSecret, "json");
        try {
            SaleFormQueryResponse response = client.excute(request);
            System.out.println("返回json/xml格式数据 :" + response.getBody());
        } catch (SuningApiException e) {
            e.printStackTrace();
        }
    }
}
